package com.redisdemo.demo.stream;

import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisStreamCommands;

import java.util.List;

public class Test2 {
    public static void main(String[] args) {
        RedisURI redisUri=RedisURI.create("localhost",6379);
        RedisClient redisClient = RedisClient.create(redisUri);
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisStreamCommands<String, String> streamCommands = connection.sync();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    List<StreamMessage<String, String>> messages = streamCommands.xreadgroup(
                            Consumer.from("lettuce-group", "线程2"),
                            XReadArgs.Builder.block(10000),
                            XReadArgs.StreamOffset.lastConsumed("first-stream"));

                    if(messages.size() == 1) {
                        System.out.println("线程2"+messages.get(0).getId()+"："+messages.get(0).getBody());
                    } else {
                        System.out.println("没有消费");

                    }
                }
            }
        }).run();
    }
}
